﻿using System;
using System.Collections.Generic;

class SequencesInMatrix
{
    static void Main()
    {
        string[,] Matrix = {{"fe", "ab", "fe", "bg",},
                            {"fe", "ab", "ab", "bg"},
                            {"cd","fe","ab","ab"}};
                           
        Console.WriteLine();
        Console.WriteLine("The matrix is:");
        for (int i = 0; i < Matrix.GetLength(0); i++)
        {
            for (int j = 0; j < Matrix.GetLength(1); j++)
            {
                Console.Write("{0,3}",Matrix[i,j]);
            }
            Console.WriteLine();
        }
        int count = 0;
        int bestLength = 1;
        List<string> bestSequence = new List<string>();
        for (int row = 0; row < Matrix.GetLength(0) - 1; row++)
        {
            for (int col = 0; col < Matrix.GetLength(1) - 1; col++)
            {
                if ((Matrix[row + 1, col] == Matrix[row, col]) || (Matrix[row, col + 1] == Matrix[row, col]) || (Matrix[row + 1, col + 1] == Matrix[row, col]))
                {
                    count++;
                }
                if (count > bestLength)
                {
                    bestLength = count;
                    bestSequence.Add(Matrix[row, col]);
                    bestSequence.Add(Matrix[row+1, col]);
                    bestSequence.Add(Matrix[row, col + 1]);
                    bestSequence.Add(Matrix[row + 1, col + 1]);
                }
            }
        }
        Console.WriteLine(bestLength);
       
    }
}

